package com.ahnchan.ContactsPlus.util;

import com.ahnchan.ContactsPlus.ContactDetail;

import android.util.Log;

public class WorldUtil {

	private static final int TYPE_PHONE = 1;
	private static final int TYPE_MOBILE = 2;
	
	public static String chagePhoneNumber(String phone, String language, String country, int type) {
		String strPhone = "";
		
		if ("KR".equals(country)) {
			strPhone = changeKorea(phone, type);
		
		} else if ("US".equals(country)) {
			strPhone = changeUnitedState(phone, type);
			
		} else {
			strPhone = phone;
		}
		
		return strPhone;
	}

	private static int checkPhone(int type) {
		int iResult = 0;
		
		if (type == ContactDetail.TYPE_MOBILE){
			iResult = TYPE_MOBILE;
		} else if (type == ContactDetail.TYPE_HOME || type == ContactDetail.TYPE_FAX) {
			iResult = TYPE_PHONE;
		}
		
		return iResult;
	}
	
	private static String changeKorea(String phone, int type) {
		String strPhone = "";
		
		try {
			if (type == TYPE_MOBILE) {
				int iSize = phone.length();
				if (iSize == 10) {
					strPhone = phone.substring(0,3) + "-" + phone.substring(3,6) + "-" + phone.substring(6, 10);
				} else if (iSize == 11) {
					strPhone = phone.substring(0,3) + "-" + phone.substring(3,7) + "-" + phone.substring(7, 11);
				} else {
					strPhone = phone;
				}
			} else if (type == TYPE_PHONE) {
				int iSize = phone.length();
				if (iSize == 7) {	
					// 1231234 (7)
					strPhone = phone.substring(0,3) + "-" + phone.substring(3,7);
				} else if (iSize == 8) {
					// 12341234 (8)
					strPhone = phone.substring(0,4) + "-" + phone.substring(4,8);
				} else if (iSize == 9) {
					// 121231234 (9)
					strPhone = phone.substring(0,2) + "-" + phone.substring(2,5) + "-" + phone.substring(5,9);
				} else if (iSize == 10) {
					// 1212341234 (10), 1231231234 (10)
					if ("02".equals(phone.substring(0,2))) {
						strPhone = phone.substring(0,2) + "-" + phone.substring(2,6) + "-" + phone.substring(6,10);
					} else {
						strPhone = phone.substring(0,3) + "-" + phone.substring(3,6) + "-" + phone.substring(6,10);
					}
				} else if (iSize == 11) {
					// 12312341234 (11)
					strPhone = phone.substring(0,3) + "-" + phone.substring(3,7) + "-" + phone.substring(7,11);
				} else {
					strPhone = phone;
				}
				
			} else {
				Log.e("ContactsPlus", "[WorldUtil] miss type. TYPE:" + type );
				strPhone = phone;
			}
			
		} catch (Exception ex) {
			Log.e("ContactPlus", "[WorldUtil] Exception(Korea) : " + phone + "," + type);
			strPhone = phone;
		}
		
		return strPhone;
	}
	
	private static String changeUnitedState(String phone, int type) {
		String strPhone = "";
		
		try {
			int iSize = phone.length();
			if (iSize == 10) {
				strPhone = phone.substring(0,3) + "-" + phone.substring(3,6) + "-" + phone.substring(6, 10);
			} else {
				strPhone = phone;
			}
		} catch (Exception ex) {
			Log.e("ContactPlus", "[WorldUtil] Exception(UnitedState) : " + phone + "," + type);
			strPhone = phone;
		}
		
		return strPhone;
	}
}